package com.qf.j2102.controller;

import com.qf.j2102.common.JsonBean;
import com.qf.j2102.po.SysAdmin;
import com.qf.j2102.po.SysCar;
import com.qf.j2102.po.SysOrder;
import com.qf.j2102.po.SysUser;
import com.qf.j2102.service.SysAdminService;
import com.qf.j2102.utils.JsonUtils;
import com.qf.j2102.utils.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private SysAdminService adminService;

    /**
     * 后台登录
     * @param a_uname
     * @param a_pword
     * @return
     */
    @RequestMapping(value = "/login.do",method = RequestMethod.POST)
    @ResponseBody
    public JsonBean login(String a_uname, String a_pword, HttpServletRequest req, HttpServletResponse resq) {
        JsonBean bean = null;
        MD5 md5 = new MD5();
        String pass = md5.getMD5ofStr(a_pword);
        try {
            SysAdmin sysAdmin = adminService.login(a_uname, pass);
            req.setAttribute("sysAdmin",sysAdmin);
            bean = JsonUtils.createJsonBean(1,sysAdmin);
        } catch (Exception e) {
            bean = JsonUtils.createJsonBean(0,e.getMessage());
        }

        return bean;
    }

    /**
     * 后台注册
     * @param sysAdmin
     * @return
     */
    @RequestMapping("/regist.do")
    @ResponseBody
    public JsonBean regist(SysAdmin sysAdmin) {
        JsonBean bean = null;
        MD5 md5 = new MD5();
        String md5ofStr = md5.getMD5ofStr(sysAdmin.getA_pword());
        sysAdmin.setA_pword(md5ofStr);
        try {
            adminService.regist(sysAdmin);
            bean = JsonUtils.createJsonBean(1,null);
        } catch (Exception e) {
            bean = JsonUtils.createJsonBean(0,e.getMessage());
        }
        return bean;
    }

    /**
     * 查询所用用户信息
     * @return
     */
    @RequestMapping(value = "/allUsers.do",method = RequestMethod.GET)
    @ResponseBody
    public JsonBean allUsers() {
        JsonBean bean = null;
        try {
            List<SysUser> sysUsers = adminService.selectAllUsers();
            bean = JsonUtils.createJsonBean(0,sysUsers);
        } catch (Exception e) {
            bean = JsonUtils.createJsonBean(1,e.getMessage());
        }
        return bean;
    }

    /**
     * 查询所有车辆信息
     * @return
     */
    @RequestMapping("/allCars.do")
    @ResponseBody
    public JsonBean allCars() {
        JsonBean bean = null;
        try {
            List<SysCar> sysCars = adminService.selectAllCars();
            bean = JsonUtils.createJsonBean(1,sysCars);
        } catch (Exception e) {
            bean = JsonUtils.createJsonBean(0,e.getMessage());
        }
        return bean;
    }

    /**
     * 查询所有订单信息
     * @return
     */
    @RequestMapping("/allOrders.do")
    @ResponseBody
    public JsonBean allOrders() {
        JsonBean bean = null;
        try {
            List<SysOrder> sysOrders = adminService.selectAllOrders();
            bean = JsonUtils.createJsonBean(1,sysOrders);
        } catch (Exception e) {
            bean = JsonUtils.createJsonBean(0,e.getMessage());
        }
        return bean;
    }
    /**
     * 批量删除用户信息
     * @return
     */
    @RequestMapping("/delUser.do")
    @ResponseBody
    public JsonBean delUser(List<Integer> ids) {
        JsonBean bean = null;
        try {
            int i = adminService.delUser(ids);
            bean = JsonUtils.createJsonBean(1,null);
        } catch (Exception e) {
            bean = JsonUtils.createJsonBean(0,e.getMessage());
        }
        return bean;
    }
    /**
     * 批量删除用户信息
     * @return
     */
    @RequestMapping("/delCar.do")
    @ResponseBody
    public JsonBean delCar(List<Integer> ids) {
        JsonBean bean = null;
        try {
            int i = adminService.delCar(ids);
            bean = JsonUtils.createJsonBean(1,null);
        } catch (Exception e) {
            bean = JsonUtils.createJsonBean(0,e.getMessage());
        }
        return bean;
    }
    /**
     * 批量删除用户信息
     * @return
     */
    @RequestMapping("/delOrder.do")
    @ResponseBody
    public JsonBean delOrder(List<Integer> ids) {
        JsonBean bean = null;
        try {
            int i = adminService.delOrder(ids);
            bean = JsonUtils.createJsonBean(1,null);
        } catch (Exception e) {
            bean = JsonUtils.createJsonBean(0,e.getMessage());
        }
        return bean;
    }
}
